<?php
namespace app\index\controller;
use think\Image;
use think\Request;
class Index extends \think\Controller
{
    public function index()
    {

        return view();
    }
    public function getFace(Request $request){
        trace("进入");
        $file = $request->file('pic');
        if (empty($file)) {
            $this->error('请选择上传文件');
        }
        //移动到upload文件夹下面，并返回路径
        $local_path = add_img($file);
        trace("上传图片的本地路径" . $local_path);
        $pic_path =LOCATION . $local_path;
        trace("上传图片的http路径" . $pic_path);

        // 读取图片
        //识别脸部矩形框
        $face_res = _request("https://api-cn.faceplusplus.com/facepp/v3/detect",true,"post",["api_key"=>"JCkvbekz-laXLQ1QWmCOUZVEH_VxbTe1","api_secret"=>"MY6O3Qy-VULxwRF-zc9414Zjl7cnpLwo","image_url"=>$pic_path]);
        $face_tem = json_decode($face_res, true);
        //识别身体矩形框
        $body_res = _request("https://api-cn.faceplusplus.com/humanbodypp/beta/detect",true,"post",["api_key"=>"JCkvbekz-laXLQ1QWmCOUZVEH_VxbTe1","api_secret"=>"MY6O3Qy-VULxwRF-zc9414Zjl7cnpLwo","image_url"=>$pic_path]);
        $body_tem = json_decode($body_res, true);
        // dump($body_tem);die;

        if(empty($face_tem["faces"][0])){
            return returnData(0,"未检测到头像");
        }else{
            $img_rect = $face_tem["faces"][0]["face_rectangle"];
            $img_width = $img_rect["width"];
            $img_height = $img_rect["height"];
            $img_left = $img_rect["left"];
            $img_top = $img_rect["top"];

            if(!empty($body_tem["humanbodies"][0])){
                $body_rect = $body_tem["humanbodies"][0]["humanbody_rectangle"];
                $body_width = $body_rect["width"];
                $body_height = $body_rect["height"];
                $body_left = $body_rect["left"];
                $body_top = $body_rect["top"];

                if($body_left<=$img_left && $body_top<=$img_top){
                    $extra_size = $img_top - $body_top;
                    if($img_left - $extra_size < 0){//如果超过了图片左边界
                        $extra_size = $img_left - $body_left;
                    }
                    if($img_top-$extra_size*1.3 < 0){//头像框上移动到了照片外面
                        $reach_top = true;
                    }
                }else{
                    $extra_size = $img_rect["width"]*0.3;
                }
            }else{
                $extra_size = $img_rect["width"]*0.3;
            }
            $img_width += $extra_size * 2;
            $img_height += $extra_size*2; 
            $img_left -= $extra_size;
            $img_top -=  empty($reach_top)? $extra_size*1.3 : $extra_size;
          
            //打开图片
            $image = Image::open(ROOT_PATH . $local_path);

            $image->crop( $img_width, $img_height , $img_left , $img_top);
            $croped_img_url = 'public/uploads/2.png';
            $image->save(ROOT_PATH . $croped_img_url);
            return returnData(1,"头像获取成功",["avatarImg"=>LOCATION ."/" .$croped_img_url]);
        }
       
    }
    public function getFacePos(Request $request){
        trace("进入getFacePos");
        $file = $request->file('pic'); 
        $outputW = input("winW");
        if (empty($file)) {
            $this->error('请选择上传文件');
            return returnData(-1,"请选择上传文件");
        }
        //移动到upload文件夹下面，并返回路径
        $local_path = add_img($file);
        trace("上传图片的本地路径" . $local_path);
        $pic_path =LOCATION . $local_path;
        trace("上传图片的http路径" . $pic_path);
        //压缩图片
        //打开图片
        $image = Image::open(ROOT_PATH . $local_path);
        $width = $image->width(); // 返回图片的宽度
        $height = $image->height();
        $width = $width/$outputW; //取得图片的长宽比  $outputW是要输出的图片的宽度
        $height = ceil($height/$width);
        $image->thumb($outputW,$height);
        $thumb_img_url = '/public/uploads/'.time().rand(0,1000000000).".png";
        $image->save(ROOT_PATH . $thumb_img_url);
        $pic_path = LOCATION . $thumb_img_url;
        $image_size =  $image->size();
        // 读取图片
        //识别脸部矩形框
        $face_res = _request("https://api-cn.faceplusplus.com/facepp/v3/detect",true,"post",["api_key"=>"JCkvbekz-laXLQ1QWmCOUZVEH_VxbTe1","api_secret"=>"MY6O3Qy-VULxwRF-zc9414Zjl7cnpLwo","image_url"=>$pic_path]);
        $face_tem = json_decode($face_res, true);
        // dump($body_tem);die;

        if(empty($face_tem["faces"][0])){
            $returnArr["size"] = $image_size;
            $returnArr["pos"] =  null;
            $returnArr["url"] =  $pic_path;
            return returnData(0,"未检测到头像,仍然有图片路径",$returnArr);
        }else{
            $img_rect = $face_tem["faces"][0]["face_rectangle"];
            $returnArr["size"] = $image_size;
            $returnArr["pos"] =  $img_rect;
            $returnArr["url"] =  $pic_path;

            return returnData(1,"返回头部坐标",$returnArr);
        } 
    }
}
